﻿using System;
using Aocsoft.Infrastructure.DomainBase;
using Iesi.Collections.Generic;

namespace AocsoftSample.PersistModels
{
    public class Category : EntityBase
    {
        public virtual string LangCode { get; set; }

        public virtual string Name { get; set; }

        /// <summary>
        /// Lvelel = 1的为顶级菜单，不允许修改
        /// </summary>
        public virtual int Level { get; set; }

        public virtual int OrderIndex { get; set; }

        public virtual Category Parent { get; set; }

        public virtual string UriCode { get; set; }

        /// <summary>
        /// Artical: 连接到Artical表中的一篇文章(About, Contact US)
        /// Customer: 按Product处理,Job, Download
        /// ArticalList: 根据当前Category类型，获取所有的文章列表（Case, News）
        /// </summary>
        [Obsolete]
        public virtual string Type { get; set; }

        public virtual ISet<Category> Children { get; set; }
        public virtual ISet<Category> Ancestors { get; set; }
        public virtual ISet<Category> Descendants { get; set; }

        public Category()
        {
            Children = new HashedSet<Category>();
            Ancestors = new HashedSet<Category>();
            Descendants = new HashedSet<Category>();
        }

        public virtual void AddChild(Category childNode)
        {
            Children.Add(childNode);
            childNode.Parent = this;
            childNode.Ancestors.AddAll(this.Ancestors);
            childNode.Ancestors.Add(this);
            childNode.LangCode = this.LangCode;
            childNode.Level = this.Level + 1;
            this.Descendants.Add(childNode);
        }

        public virtual void RemoveChild(Category childNode)
        {
            Children.Remove(childNode);
            this.Descendants.Remove(childNode);
        }
    }

    public class Node
    {
        public virtual Guid Id { get; set; }
        public virtual string Name { get; set; }
        public virtual Node Parent { get; set; }
        public virtual ISet<Node> Children { get; set; }

        public virtual ISet<Node> Ancestors { get; set; }
        public virtual ISet<Node> Descendants { get; set; }

        public Node()
        {
            Children = new HashedSet<Node>();
            Ancestors = new HashedSet<Node>();
            Descendants = new HashedSet<Node>();
        }

        public virtual void AddChild(Node childNode)
        {
            Children.Add(childNode);
            childNode.Parent = this;
            childNode.Ancestors.AddAll(this.Ancestors);
            childNode.Ancestors.Add(this);
            this.Descendants.Add(childNode);
        }
    }
}
